﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Xenta.Utils;

namespace Xenta.Web.Controllers
{
    public sealed class SearchController : ExtController
    {
        [HttpGet]
        public ActionResult Search(string searchTerm)
        {
            var m = new Dictionary<String,dynamic>
            {
                {
                    "News", Svc.ProcessOperation("SearchPublications", new
                    {
                        LanguageID = JDM.Int32(Svc.Localization.Language.EntityID),
                        Term = new
                        {
                            Expression = String.Format("%{0}%", searchTerm)
                        },
                        Count = 5,
                        PublishedTo = DateTime.UtcNow,
                        FlagMask = "Active,News",
                        SortBy = new[] 
                        {
                            "PublishedOn"
                        },
                        SortDir = "Descending"
                    }, new
                    {
                        DisclosureLevel = "Extended",
                        DisclosureOptions = new Dictionary<String, String>
                        {
                            { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                        }.Unload()
                    })
                },
                {
                    "Forum", Svc.ProcessOperation("SearchForumTopics", new
                    {
                        Term = new
                        {
                            Expression = String.Format("%{0}%", searchTerm)
                        },
                        Count = 5,
                        SortBy = new[] 
                        {
                            "CreatedOn"
                        },
                        SortDir = "Descending"
                    }, new
                    {
                        DisclosureLevel = "Extended"
                    })
                },
                {
                    "Blog", Svc.ProcessOperation("SearchPublications", new
                    {
                        LanguageID = JDM.Int32(Svc.Localization.Language.EntityID),
                        Term = new
                        {
                            Expression = String.Format("%{0}%", searchTerm)
                        },
                        Count = 5,
                        PublishedTo = DateTime.UtcNow,
                        FlagMask = "Active,BlogPost",
                        SortBy = new[] 
                        {
                            "PublishedOn"
                        },
                        SortDir = "Descending"
                    }, new
                    {
                        DisclosureLevel = "Extended",
                        DisclosureOptions = new Dictionary<String, String>
                        {
                            { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                        }.Unload()
                    })
                },
                {
                    "Catalog", Svc.ProcessOperation("SearchCatalogItems", new
                    {
                        LanguageID = JDM.Int32(Svc.Localization.Language.EntityID),
                        Term = new
                        {
                            Expression = String.Format("%{0}%", searchTerm)
                        },
                        Count = 5,
                        FlagMask = "Active",
                        SortBy = new[] 
                        {
                            "CreatedOn"
                        },
                        SortDir = "Descending"
                    }, new
                    {
                        DisclosureLevel = "Extended",
                        DisclosureOptions = new Dictionary<String, String>
                        {
                            { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                        }.Unload()
                    })
                },
                {
                    "Store", Svc.ProcessOperation("SearchSalesItemsOverCatalog", new
                    {
                        LanguageID = JDM.Int32(Svc.Localization.Language.EntityID),
                        Term = new
                        {
                            Expression = String.Format("%{0}%", searchTerm)
                        },
                        AvailableOn = DateTime.UtcNow,
                        Count = 5,
                        FlagMask = "Active",
                        SalesSortBy = new[] 
                        {
                            "CreatedOn"
                        },
                        SortDir = "Descending"
                    }, new
                    {
                        DisclosureLevel = "Extended",
                        DisclosureOptions = new Dictionary<String, String>
                        {
                            { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                        }.Unload()
                    })
                }
            };
            return View(m);
        }
    }
}
